package com.wu.client.schedule;

import com.wu.client.cache.ConstantData.*;
import com.wu.client.constant.*;
import com.wu.client.vo.res.ConstantResp;
import com.wu.client.vo.res.playlist.PlaylistCategoriesResp;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author Cousin
 * @date 2025/3/28
 * @Description
 */
@Component
public class ConstantSchedule {
    @PostConstruct
    public void onStartup() {
        // 启动时立即调用任务
        updateConstant();
    }
    @Scheduled(cron = "0 0 0 * * ?")
    public void updateConstant(){
        Map<String, List<ConstantResp>> regionLanguageMap = new HashMap<>();
        List<ConstantResp> languages = new ArrayList<>();
        for (LanguageEnum languageEnum : LanguageEnum.values()) {
            ConstantResp constantResp = new ConstantResp();
            constantResp.setId(languageEnum.getCode());
            constantResp.setName(languageEnum.getLanguage());
            languages.add(constantResp);
        }
        regionLanguageMap.put("languages", languages);
        List<ConstantResp> regions = new ArrayList<>();
        for (RegionEnum regionEnum : RegionEnum.values()) {
            ConstantResp constantResp = new ConstantResp();
            constantResp.setId(regionEnum.getCode());
            constantResp.setName(regionEnum.getRegion());
            regions.add(constantResp);
        }
        regionLanguageMap.put("regions", regions);
        RegionAndLanguageCache.getInstance().setMap(regionLanguageMap);
       //-------------------------------------------------
        Map<String, List<ConstantResp>> artistTypeMap = new HashMap<>();
        List<ConstantResp> artistTypes = new ArrayList<>();
        for (ArtistTypeEnum artistTypeEnum : ArtistTypeEnum.values()) {
            ConstantResp constantResp = new ConstantResp();
            constantResp.setId(artistTypeEnum.getCode());
            constantResp.setName(artistTypeEnum.getType());
            artistTypes.add(constantResp);
        }
        artistTypeMap.put("artistTypes", artistTypes);
        ArtistTypeCache.getInstance().setMap(artistTypeMap);
        //-------------------------------------------------
        Map<String, List<ConstantResp>> albumGenreMap = new HashMap<>();
        List<ConstantResp> albumGenres = new ArrayList<>();
        for (AlbumGenreEnum albumGenreEnum : AlbumGenreEnum.values()) {
            ConstantResp constantResp = new ConstantResp();
            constantResp.setId(albumGenreEnum.getCode());
            constantResp.setName(albumGenreEnum.getGenre());
            albumGenres.add(constantResp);
        }
        albumGenreMap.put("albumGenres", albumGenres);
        AlbumGenresCache.getInstance().setMap(albumGenreMap);
        //---------------------------------------------------
        Map<String, List<ConstantResp> > mvTypesMap = new HashMap<>();
        List<ConstantResp> mvTypes = new ArrayList<>();
        for (MvVersion mvVersion : MvVersion.values()) {
            ConstantResp constantResp = new ConstantResp();
            constantResp.setId(mvVersion.getCode());
            constantResp.setName(mvVersion.getVersion());
            mvTypes.add(constantResp);
        }
        mvTypesMap.put("mvTypes", mvTypes);
        MvTypesCache.getInstance().setMap(mvTypesMap);

        // ---------------------------------------------------
        PlaylistCategoriesResp playlistCategoriesResp =new PlaylistCategoriesResp();

        // 构建 categories 数据
        Map<Integer, List<PlaylistCategoriesResp.CategoryInfo>> categories = new HashMap<>();
        for (PlaylistCategories.Category category : PlaylistCategories.Category.values()) {
            if (category.getCategoryType() != PlaylistCategories.CategoryType.OTHER) {
                PlaylistCategoriesResp.CategoryInfo categoryInfo = new PlaylistCategoriesResp.CategoryInfo();
                categoryInfo.setId(category.getId());
                categoryInfo.setName(category.getName());
                categories.computeIfAbsent(category.getCategoryType().getCode(), k -> new ArrayList<>())
                        .add(categoryInfo);
            }
        }
        playlistCategoriesResp.setCategories(categories);

        // 构建 moreCategories 数据 (属于 CategoryType.OTHER 的数据)
        Map<Integer, List<PlaylistCategoriesResp.CategoryInfo>> moreCategories = new HashMap<>();
        for (PlaylistCategories.Category category : PlaylistCategories.Category.values()) {
            if (category.getCategoryType() == PlaylistCategories.CategoryType.OTHER) {
                PlaylistCategoriesResp.CategoryInfo categoryInfo = new PlaylistCategoriesResp.CategoryInfo();
                categoryInfo.setId(category.getId());
                categoryInfo.setName(category.getName());
                moreCategories.computeIfAbsent(PlaylistCategories.CategoryType.OTHER.getCode(), k -> new ArrayList<>())
                        .add(categoryInfo);
            }
        }
        playlistCategoriesResp.setMoreCategories(moreCategories);
        // 构建 categoryNames 数据
        Map<Integer, String> categoryNames = Arrays.stream(PlaylistCategories.CategoryType.values())
                .collect(Collectors.toMap(PlaylistCategories.CategoryType::getCode, PlaylistCategories.CategoryType::getName));
        playlistCategoriesResp.setCategoryNames(categoryNames);
        PlaylistGenresCache.getInstance().setPlaylistCategoriesResp(playlistCategoriesResp);
    }

}
